package com.castle.fortress.admin.study.course.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.member.dto.MemberDto;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.study.course.entity.StudyCourseEntity;
import com.castle.fortress.admin.study.course.dto.StudyCourseDto;
import com.castle.fortress.admin.study.course.mapper.StudyCourseMapper;
import com.castle.fortress.admin.study.course.service.StudyCourseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import com.castle.fortress.admin.utils.BizCommonUtil;

/**
 * 课程管理 服务实现类
 *
 * @author castle
 * @since 2023-01-30
 */
@Service
public class StudyCourseServiceImpl extends ServiceImpl<StudyCourseMapper, StudyCourseEntity> implements StudyCourseService {
    /**
    * 获取查询条件
    * @param studyCourseDto
    * @return
    */
    public QueryWrapper<StudyCourseEntity> getWrapper(StudyCourseDto studyCourseDto){
        QueryWrapper<StudyCourseEntity> wrapper= new QueryWrapper();
        if(studyCourseDto != null){
            StudyCourseEntity studyCourseEntity = ConvertUtil.transformObj(studyCourseDto,StudyCourseEntity.class);
            wrapper.like(StrUtil.isNotEmpty(studyCourseEntity.getCourseName()),"course_name",studyCourseEntity.getCourseName());
            wrapper.eq(studyCourseEntity.getCourseStatus() != null,"course_status",studyCourseEntity.getCourseStatus());
            wrapper.eq(studyCourseEntity.getClassStatus() != null,"class_status",studyCourseEntity.getClassStatus());
            wrapper.eq(studyCourseEntity.getIsRecommend() != null,"is_recommend",studyCourseEntity.getIsRecommend());
            wrapper.last(studyCourseEntity.getClassifyId()!=null,"AND classify_id in (" +
                    "                SELECT DISTINCT id FROM study_course_classify WHERE id= "+studyCourseEntity.getClassifyId()+" or parents like concat('%',"+studyCourseEntity.getClassifyId()+",'%')\n" +
                    "                )"+
                    "ORDER BY create_time");
        }else{
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<StudyCourseDto> pageStudyCourse(Page<StudyCourseDto> page, StudyCourseDto studyCourseDto) {
        QueryWrapper<StudyCourseEntity> wrapper = getWrapper(studyCourseDto);
        Page<StudyCourseEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<StudyCourseEntity> studyCoursePage=baseMapper.selectPage(pageEntity,wrapper);
        Page<StudyCourseDto> pageDto = new Page(studyCoursePage.getCurrent(), studyCoursePage.getSize(),studyCoursePage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(studyCoursePage.getRecords(),StudyCourseDto.class));
        return pageDto;
    }

	@Override
	public IPage<StudyCourseDto> pageStudyCourseExtends(Page<StudyCourseDto> page, StudyCourseDto studyCourseDto){
		Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
          StudyCourseEntity studyCourseEntity = ConvertUtil.transformObj(studyCourseDto,StudyCourseEntity.class);
		List<StudyCourseEntity> studyCourseList=baseMapper.extendsList(pageMap,studyCourseEntity);
		Long total = baseMapper.extendsCount(studyCourseEntity);
		Page<StudyCourseDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
		pageDto.setRecords(ConvertUtil.transformObjList(studyCourseList,StudyCourseDto.class));
		return pageDto;
	}
	@Override
	public StudyCourseDto getByIdExtends(Long id){
          StudyCourseEntity  studyCourseEntity = baseMapper.getByIdExtends(id);
		return ConvertUtil.transformObj(studyCourseEntity,StudyCourseDto.class);
	}

    @Override
    public List<StudyCourseDto> listStudyCourse(StudyCourseDto studyCourseDto){
        QueryWrapper<StudyCourseEntity> wrapper = getWrapper(studyCourseDto);
        List<StudyCourseEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,StudyCourseDto.class);
    }

    @Override
    public boolean updateOpt(Long id, String type, String val) {
        //修改置顶状态
        if("00".equals(type)){
            baseMapper.updateRecommend(id,Integer.parseInt(val));
            //修改课程状态
        }else if("01".equals(type)){
            baseMapper.updateCoursestatus(id,Integer.parseInt(val));
        }else if("02".equals(type)){
            baseMapper.updateRecommendType(id,val);
        }
        return true;
    }

    @Override
    public List<StudyCourseDto> scheduleList(StudyCourseDto studyCourseDto, MemberDto member) {
        List<StudyCourseEntity> list = baseMapper.scheduleList(studyCourseDto,member);
        return ConvertUtil.transformObjList(list,StudyCourseDto.class);
    }

    @Override
    public List<StudyCourseDto> finishList(StudyCourseDto studyCourseDto, MemberDto member) {
        List<StudyCourseEntity> list = baseMapper.finishList(studyCourseDto,member);
        return ConvertUtil.transformObjList(list,StudyCourseDto.class);
    }

    @Override
    public StudyCourseDto studyInfo(Long id,Long memberId) {
        StudyCourseEntity entity = baseMapper.studyInfo(id,memberId);
        return ConvertUtil.transformObj(entity,StudyCourseDto.class);
    }

    @Override
    public List<StudyCourseDto> collectList(Long memberId) {
        List<StudyCourseEntity> list = baseMapper.collectList(memberId);
        return ConvertUtil.transformObjList(list,StudyCourseDto.class);
    }

    @Override
    public void delByClassifyId(Long id) {
        if(id!=null){
            baseMapper.delByClassifyId(id);
        }

    }

    @Override
    public IPage<StudyCourseDto> memberStudyPage(Page<StudyCourseDto> page, StudyCourseDto studyCourseDto) {
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        StudyCourseEntity studyCourseEntity = ConvertUtil.transformObj(studyCourseDto,StudyCourseEntity.class);
        List<StudyCourseEntity> studyCourseList=baseMapper.memberStudyList(pageMap,studyCourseEntity);
        Long total = baseMapper.memberStudyCount(studyCourseEntity);
        Page<StudyCourseDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(studyCourseList,StudyCourseDto.class));
        return pageDto;
    }

    @Override
    public void updateAllStatus(Long courseId, Integer courseStatus, Integer isRecommend) {
        baseMapper.updateAllStatus(courseId,courseStatus,isRecommend);
    }
}

